package com.bys.admin.ai.controller;

import cn.hutool.core.util.ObjUtil;
import com.bys.admin.ai.controller.vo.ChatMessagePageReqVO;
import com.bys.admin.ai.controller.vo.ChatMessageSaveReqVO;
import com.bys.admin.ai.controller.vo.ChatMessageSendVO;
import com.bys.admin.ai.entity.Chat;
import com.bys.admin.ai.entity.ChatMessage;
import com.bys.admin.ai.service.ChatMessageService;
import com.bys.admin.ai.service.ChatService;
import com.bys.response.R;
import com.bys.security.SecurityUtils;
import com.bys.util.BeanUtils;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import javax.validation.constraints.*;
import javax.validation.*;

import java.util.*;
import java.io.IOException;

import com.bys.vo.PageVo;
import reactor.core.publisher.Flux;

import static com.bys.response.R.success;

@Tag(name = "管理后台 - AI Chat 消息")
@RestController
@RequestMapping("/ai/chat-message")
@Validated
public class ChatMessageController {

    @Resource
    private ChatMessageService chatMessageService;
    @Resource
    private ChatService chatService;
    @Operation(summary = "发送消息（流式）", description = "流式返回，响应较快")
    @PostMapping(value = "/send-stream", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<Boolean> sendChatMessageStream( @RequestBody ChatMessageSendVO chatMessageSendVO ) {
        return chatMessageService.sendChatMessageStream(chatMessageSendVO, SecurityUtils.getUserId());
     }
    @Operation(summary = "获得指定对话的消息列表")
    @GetMapping("/list-by-chat-id/{chatId}")
    @Parameter(name = "conversationId", required = true, description = "对话编号", example = "1024")
    public R<List<ChatMessage>> getChatMessageListByConversationId(@PathVariable Long chatId) {
        Chat chat = chatService.getById(chatId);
        if (Objects.isNull(chat) || ObjUtil.notEqual(chat.getUserId(), SecurityUtils.getUserId())) {
            return success(Collections.emptyList());
        }
        List<ChatMessage> messageList = chatMessageService.listByChatId(chatId);
        return success(messageList);
    }

    @PostMapping("/create")
    @Operation(summary = "创建AI Chat 消息")
    public R<Boolean> createChatMessage(@Valid @RequestBody ChatMessageSaveReqVO createReqVO) {
    chatMessageService.createChatMessage(createReqVO);
    return success(true);
    }

    @PutMapping("/update")
    @Operation(summary = "更新AI Chat 消息")
    public R<Boolean> updateChatMessage(@Valid @RequestBody ChatMessageSaveReqVO updateReqVO) {
        chatMessageService.updateChatMessage(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete/{id}")
    @Operation(summary = "删除AI Chat 消息")
    @Parameter(name = "id", description = "编号", required = true)
    public R<Boolean> deleteChatMessage(@PathVariable Long id) {
        chatMessageService.deleteChatMessage(id);
        return success(true);
    }

    @GetMapping("/get/{id}")
    @Operation(summary = "获得AI Chat 消息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public R<ChatMessage> getChatMessage(@PathVariable Long id) {
        ChatMessage ChatMessage = chatMessageService.getChatMessage(id);
        return success(ChatMessage);
    }

    @GetMapping("/page")
    @Operation(summary = "获得AI Chat 消息分页")
    public R<Page<ChatMessage>> getChatMessagePage(@Valid ChatMessagePageReqVO pageReqVO) {
        Page<ChatMessage> pageResult = chatMessageService.getChatMessagePage(pageReqVO);
        return success(pageResult);
    }
}